package api

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"k8s-go/common"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

type K8s struct {
}

func (*K8s) GetK8sNamespace(c *gin.Context) {
	r := common.Res{}
	config, err := clientcmd.BuildConfigFromFlags("", "config")
	if err != nil {
		r.Fail(c, err.Error())
		return
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		r.Fail(c, err.Error())
		return
	}
	result, err := clientset.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{})
	for _, item := range result.Items {
		fmt.Println(item.Namespace, item.Name)
	}
	r.DataSuccessWithMsg(c, "success", result)
}
